package com.eightbears.beboy.base.view.popupwindow;

import android.app.Activity;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.TagPopAdapter;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.basepopu.BasePopupWindow;
import com.eightbears.beboy.entity.TagsEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.google.gson.Gson;
import com.socks.library.KLog;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_ADDSHOPTECHNICIAN_TAGS;


public class TagsPopup extends BasePopupWindow {

    private View popupView;
    private RecyclerView rv_tags;
    private Button btn_send;
    private TextView tv_hongbao_get;
    private LinearLayout ll_bottom_menu;
    private OnClickItemListener mOnClicktemListener;
    private TagPopAdapter mTagPopAdapter;
    private ArrayList<TagsEntity.DataBean> list = new ArrayList<TagsEntity.DataBean>();


    public TagsPopup(Activity context) {
        super(context);
        bindEvent();
        initAdapter();
        initData();
    }

    @Override
    public Animation getAnimation() {
        return getDefaultAlphaAnimation();
    }

    @Override
    public AnimationSet getAnimationSet() {
        return null;
    }

    @Override
    public View getInputView() {
        return null;
    }

    @Override
    public View getDismissView() {
        return popupView.findViewById(R.id.click_to_dismiss);
    }

    @Override
    public View getPopupView() {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.view_tags_popup, null);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    private void bindEvent() {
        if (popupView != null) {
            rv_tags = (RecyclerView) popupView.findViewById(R.id.rv_tags);
            btn_send = (Button) popupView.findViewById(R.id.btn_send);
        }
    }


    private void initAdapter() {
        final GradientDrawable gd = new GradientDrawable();//创建drawable
        gd.setCornerRadius(5);
        mTagPopAdapter = new TagPopAdapter(mContext);
        rv_tags.setLayoutManager(new GridLayoutManager(mContext, 3));
        mTagPopAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                TagsEntity.DataBean dataBean = (TagsEntity.DataBean) adapter.getData().get(position);

                switch (view.getId()) {
                    case R.id.tv_tags:
                        if (dataBean.isCheck()) {//如果选择
                            KLog.e("进来1");
                            dataBean.setCheck(false);
                            list.remove(dataBean);
                            KLog.e(list.size() + "数量");
                        } else {
                            KLog.e("进来2");
                            dataBean.setCheck(true);
                            list.add(dataBean);
                            if (list.size() > 3) {
                                dataBean.setCheck(false);
                                ShowToast.showShortToast("标签最多只能3个");
                                list.remove(dataBean);
                            }
                            KLog.e(list.size() + "数量");
                        }
                        mTagPopAdapter.notifyDataSetChanged();
                        break;
                }
            }
        });
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnClicktemListener.onClickItem(list, v);
            }
        });
    }

    private void initData() {
        //获取accesstoken
        OkHttpUtils.get().url(URL_ADDSHOPTECHNICIAN_TAGS)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("type", new Gson().toJson(2))
                .build().execute(new StringCallback() {
            //TODO
            @Override
            public void onResponse(String response) {
                KLog.e(response);
                try {
                    TagsEntity tagsEntity = GsonUtil.fromJson(response, TagsEntity.class);
                    List<TagsEntity.DataBean> data = tagsEntity.getData();
                    mTagPopAdapter.setNewData(data);
                    rv_tags.setAdapter(mTagPopAdapter);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call request, Exception e) {
                KLog.e(e.getMessage());
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(mContext, status_error);
            }
        });
    }

    public interface OnClickItemListener {
        void onClickItem(ArrayList<TagsEntity.DataBean> list, View v);
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.mOnClicktemListener = onClickItemListener;
    }

}
